<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Python lexical structure</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Python, lexical structure, programming, language, learn Python">
<meta name="description" content="In this part of the Python tutorial, we will
talk about the lexical structure of the Python language.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Python lexical structure</h1>

<p>
Computer languages, like human languages, have a lexical structure. A source 
code of a Python program consists of tokens. Tokens are atomic code elements. 
In Python language, we have comments, variables, literals, operators, delimiters 
and keywords.
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

<h2>Comments</h2>

<p>
<b>Comments</b> are used by humans to clarify the source code. 
All comments in Python language follow
the <b>#</b> character. 
</p>

<pre class="code">
#!/usr/bin/python

# commemts.py
# author jan bodnar
# ZetCode 2008

def main():
    print "Comments example"

main()
</pre>

<p>
Everything that follows the # character is ignored by the Python interpreter. 
</p>


<h2>Variables</h2>

<p>
A <b>variable</b> is an identifier, which holds a value. In 
programming we say, that we assign a value to a variable. Technically speaking, 
a variable is a reference to a computer memory, where the value is stored. 
In Python language, a variable can hold a string, a number or various objects 
like a function or a class. Variables can be assigned 
different values over time. 
</p>

<p>
Variables in python can be created from alfanumeric characters and 
underscore (_) character. A variable cannot begin
with a number. The Python interpreter can easier distinguish 
between a number and a variable. 
</p>

<pre>
Value
value2
company_name
</pre>

<p>
These were valid identifiers. 
</p>

<pre>
12Val
exx$
first-name
</pre>

<p>
These were examples of invalid identifiers. 
</p>


<p>
The variables are <b>case sensitive</b>. This means, that  <code>Price</code>, 
<code>price</code>, and <code>PRICE</code>
are three different identifiers. 
</p>

<pre class="code">
#!/usr/bin/python

number = 10
Number = 11
NUMBER = 12

print number, Number, NUMBER
</pre>

<p>
In our script, we assign three numeric values to three identifiers. 
</p>

<pre>
10 11 12
</pre>

<p>
This is the output of the script. 
</p>


<h2>A literal</h2>

<p>
A <b>literal</b> is any notation for representing a value 
within the Python source code. Technically, a literal will be assigned a value 
at compile time, while a variable  will be assigned at runtime.
</p>

<pre>
age = 29
nationality = "Hungarian"
</pre>

<p>
Here we assign two literals to variables. Number 29 and string Hungarian are literals. 
</p>

<pre class="code">
#!/usr/bin/python

# literals.py

name1 = "Jane"
age1 = 12

name2 = "Rose"
age2 = 16

"Patrick"
34

"Luke"
23

print name1, age1
print name2, age2
</pre>

<p>
If we do not assign a literal to a variable, there is no way, how 
we can work with it. It is dropped. 
</p>

<pre>
$ ./literals.py 
Jane 12
Rose 16
</pre>

<p>
This is the output of the literals.py script. 
</p>


<h2>Operators</h2>

<p>
An <b>operator</b> is a symbol used to perform an 
action on some value. 
</p>

<pre>
+    -    ~    *    **    /    %
&lt;&lt;    >>    &amp;    |    ^
and    or    not    in    not in 
is    is not    &lt;    >    !=    &lt;>
==    &lt;=    >=
</pre>

<p>
This is a list of operators available in Python language. We will talk 
about operators later in the tutorial.
</p>


<h2>Indentation</h2>

<p>
Indentation is used to delimit blocks in Python. Where other programming
languages use curly brackets or keywords such as begin, end, Python uses
white space. An increase in indentation comes after certain statements; 
a decrease in indentation signifies the end of the current block.
The Python style guide recommends using 4 spaces per indentation level.
</p>

<pre>
if age &gt; 18:
    print "adult person"

for i in range(5):
    print i
</pre>

<p>
After the if keyword a code block is expected. A new statement is started 
on a new line, indented with 4 space characters. The indentation for the
following for keyword is decreased back to the initial one. The for keyword
starts a new code block, where its statement(s) are indented. 
</p>


<h2>Delimiters</h2>

<p>
A <b>delimiter</b> is a sequence of one or more characters 
used to specify the boundary between separate, independent regions in plain 
text or other data stream. 
</p>

<pre>
(       )       [       ]       {       }      
,       :       .       `       =       ;
+=      -=      *=      /=      //=     %=
&lt;=      |=      ^=      >>=     &lt;&lt;=     **=
'       "       \       @
</pre>

<p>
Delimiters are used in various area of the Python language. They are used
to build expressions, string literals, tuples, dictionaries or lists.
</p>


<h2>Keywords</h2>

<p>
A keyword is a reserved word in the Python programming language. Keywords 
are used to perform a specific task in a computer program. For example, 
print a value to the console, do repetitive tasks or perform logical operations. 
A programmer cannot use a keyword as an ordinary variable. 
</p>

<pre>
and       del       from      not       while
as        elif      global    or        with
assert    else      if        pass      yield
break     except    import    print
class     exec      in        raise
continue  finally   is        return 
def       for       lambda    try
</pre>

<p>
This is a list of Python keywords. We have dedicated a special chapter to 
keywords in this tutorial. 
</p>

<p>
This was the Python lexical structure. 
</p>



<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified December 17, 2012  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

